﻿using UnityEngine;
using UnityEngine.UI;

public class ImageSize : NodeSize
{
    public RectTransform rectTransform;
    public Image image;

    protected override void OnRefresh(float rw, float rh, float w, float h, float s)
    {
        var ws = w / (image.preferredWidth * s);
        var hs = h / (image.preferredHeight * s);
        if (ws > hs)
            SetSize(ws);
        else
            SetSize(hs);
    }

    private void SetSize(float scale)
    {
        // rectTransform.localScale = new Vector3(scale, scale, scale);
        rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, image.preferredWidth * scale);
        rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, image.preferredHeight * scale);
    }
}